<?php
/**
 * Description of UserMapper
 *
 /*
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class Application_Model_ResponsibleMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Responsibles';
    }

    /**
     * @param integer $id
     * @return Application_Model_Promotion
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('responsible_id = ?' => $id));

        //return $this->_toPromotion($row);
        return $row;
    }

    /**
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();
        
        return $this->_toResponsibles($rows);
    }

    /**
     * @param Application_Model_Responsible $Responsible
     */
    public function save(Application_Model_Responsible $Responsible)
    {
        $data = array(
            'responsible_givenname'  => $Responsible->Givenname,
            'responsible_familyname' => $Responsible->Familyname,
            'responsible_fotoUrl'    => $Responsible->FotoUrl,
        );

        if (null == $Responsible->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('responsible_id = ?' => $Responsible->Id));
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Responsible 
     */
    private function _toResponsible(Zend_Db_Table_Row_Abstract $row = null)
    {
        $responsible = new Application_Model_Responsible();

        
        if ($row) {
            $responsible->Id          = $row['responsible_id'];
            $responsible->Givenname   = $row['responsible_givenname'];
            $responsible->Familyname  = $row['responsible_familyname'];
            $responsible->FotoUrl     = $row['responsible_fotoUrl'];
        }
        
        return $responsible;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toResponsibles(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $responsibles = array();

        if ($rows) {
            foreach ($rows as $row) {
                $responsibles[] = $this->_toResponsible($row);
            }
        }
        
        return $responsibles;
    }

}